import Vue from 'vue'
import App from './App'
import store from './store' // 数据仓库

import router from './router/index.js'

import bgy from 'bgy'
import getParameterMap from './tool/getParameterMap'
import toThousands from './tool/toThousands'

Vue.use(bgy.install)

Vue.prototype.$store = store
Vue.prototype.$getParameterMap = getParameterMap
Vue.prototype.$toThousands = toThousands

// 5分钟
Vue.prototype.$pollingTime = 5

import Frame from './components/frame/frame.vue'
Vue.component(Frame.name, Frame)

import video from './components/video/video.vue'
Vue.component(video.name, video)

import mulvideo from './components/video/multiple_video.vue'
Vue.component(mulvideo.name, mulvideo)

import ProgressBar from './components/process/ProgressBar.vue'
Vue.component(ProgressBar.name, ProgressBar)

import marquee from './components/marquee/marquee.vue'
Vue.component(marquee.name, marquee)

import retrieve from './components/retrieve/retrieve.vue'
Vue.component(retrieve.name, retrieve)

import weather from './components/weather/weather.vue'
Vue.component(weather.name, weather)

import history from './components/history/history.vue'
Vue.component(history.name, history)

import numberAn from './components/numberAn/numberAn.vue'
Vue.component(numberAn.name, numberAn)

switch (process.env.NODE_TYPE) {
  case 'test':
    Vue.prototype.$site = Vue.prototype.SERVER_TEST
    break
  case 'pinmingTest':
    Vue.prototype.$site = Vue.prototype.SERVER_PINMING_TEST
    break
  case 'pre':
    Vue.prototype.$site = Vue.prototype.SERVER_PRE
    break
  case 'prod':
    Vue.prototype.$site = Vue.prototype.SERVER_PROD
    break
  case 'k8':
    Vue.prototype.$site = Vue.prototype.SERVER_PINMING_K8_TEST
    break
  case 'dev':
    if (!Vue.prototype.$getCookie('server')) {
      Vue.prototype.$site = Vue.prototype.SERVER_DEV
    } else {
      Vue.prototype.$site = JSON.parse(Vue.prototype.$getCookie('server'))
    }
    break
  default:
    if (!Vue.prototype.$getCookie('server')) {
      Vue.prototype.$site = Vue.prototype.SERVER_DEV
    } else {
      Vue.prototype.$site = JSON.parse(Vue.prototype.$getCookie('server'))
    }
    break
}
Vue.prototype.$setCookie('server', JSON.stringify(Vue.prototype.$site), {expires: 14, domain: Vue.prototype.$site.domain})

import echarts from 'echarts'
import 'echarts-liquidfill'

import {resetEcharts} from './tool/resetEcharts.js'
Vue.prototype.$resetEcharts = resetEcharts

import {fullScreen, fullExit} from './tool/fullScreen.js'
Vue.prototype.$fullScreen = fullScreen
Vue.prototype.$fullExit = fullExit
Vue.prototype.$echarts = echarts

import node from './node/node.js'
Vue.prototype.$Node = node

// 全局路由拦截
import interceptionRoute from './interceptionRoute'
router.beforeEach((to, from, next) => {
  interceptionRoute(to, from, next)
})

Vue.prototype.$axios.interceptors.request.use((config) => {
  // 防止重复请求
  if (Vue.prototype._requestSet.has(config.url) && !config.closeSaveRequest) {
    console.log(`重复请求的URL${config.url}`)
    return
  }
  Vue.prototype._requestSet.add(config.url)

  // 是否关闭加载动画
  // if (!config.closeLoading) {
  //   store.commit('showLoading')
  // }

  // 是否打开分页
  if (config.paginate) {
    // 带query的为老版本 queryType为1,不带query的为新版本 queryType为2
    const queryPaginate = store.getters.getQueryPaginate
    let currentPage = `&page=${queryPaginate.currentPage}`
    let pageSize = `&pageSize=${queryPaginate.pageSize}`
    // query 是否另外命名
    if (config.queryName) {
      currentPage = `&${config.queryName}.currentPage=${queryPaginate.currentPage}`
      pageSize = `&${config.queryName}.pageSize=${queryPaginate.pageSize}`
    }
    config.data = config.data + currentPage + pageSize
  }
  return config
})

Vue.prototype.$axios.interceptors.response.use(data => {
  Vue.prototype._requestSet.delete(data.config.url)
  if (Vue.prototype._requestSet.size === 0) {
    store.commit('hideLoading')
  }
  // 设置分页参数
  if (data.config.paginate) {
    store.commit({
      type: 'muPaginate',
      query: data.data.query || data.data.data,
      pagingId: data.config.paginId || data.config.pagingId
    })
  }
  store.commit('removeQueryPaginate')
  return data
}, (error) => {
  if (error.config) {
    Vue.prototype._requestSet.delete(error.config.url)
  }
  if (error.config && error.config.closeMessage) {
  } else {
    if (error.response) {
      if (error.response.data.error) {
        Vue.prototype.$message.error(error.response.data.error)
      }
      if (error.response.data.errorMsg) {
        Vue.prototype.$message.error(error.response.data.errorMsg)
      }
    }
  }
  store.commit('hideLoading')
  return Promise.reject(error)
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  store,
  router,
  template: '<App/>',
  components: { App }
})
